// 通用的方法
import { getTableBaseData } from '@/api/tableApi.js'
import { cities, region } from '@/mock/jsModule/table.js'
import { tableBaseData } from '@/mock/jsModule/table.js'

export const publicController = {
  data(){
    return {

    }
  },
  computed:{

  },
  methods:{
    // 获取表格数据
    async getTableData(){
      this.loading = true
      const tbData = tableBaseData
      for(let i = 0; i < 100; i++){
        // 生成0-4的随机整数
        const index = Math.floor(Math.random() * 5)
        this.tableData.push({
          id: i,
          name: tbData.name + i,
          gender: i % 2 == 0 ? 0 : 1,
          age: tbData.age,
          account: i % 3 == 0 ? 'admin' : 'user',
          city: cities[index],
          region: region[index],
          address: cities[index] + region[index] + '朝阳路' + i + '号',
          date: tbData.date,
          checked: true,
          postalCode: tbData.postalCode + i,
          edit: tbData.edit
        })
      }
      // 注释mock请求
      // const res = await getTableBaseData()
      // if(res.status == 200){
      //   const data = res.data
      //   for(let i = 0; i < 100; i++){
      //     // 生成0-4的随机整数
      //     const index = Math.floor(Math.random() * 5)
      //     this.tableData.push({
      //       id: i,
      //       name: data.name + i,
      //       gender: i % 2 == 0 ? 0 : 1,
      //       age: data.age,
      //       account: i % 3 == 0 ? 'admin' : 'user',
      //       city: cities[index],
      //       region: region[index],
      //       address: cities[index] + region[index] + '朝阳路' + i + '号',
      //       date: data.date,
      //       checked: true,
      //       postalCode: data.postalCode + i,
      //       edit: data.edit
      //     })
      //   }
      // }
      this.loading = false
    },
    randomNumber(){
      let min = 10000000
      let max = 99999999
      let _this = this
      let finalNum = 0
      function createRandomNumber(){
        let num = Math.floor((Math.random() * (max - min)) + min)
        if(_this.randomNumberList.indexOf(num) == -1){  // 不在现有的随机数数组中
          _this.randomNumberList.push(num)
          finalNum = num
        }else{
          createRandomNumber()
        }
      }
      createRandomNumber()
      return finalNum
    },
    // 生成指定条数
    getPartData(num){
      this.loading = true
      const tbData = tableBaseData
      for(let i = 0; i < num; i++){
        // 生成0-4的随机整数
        const index = Math.floor(Math.random() * 5)
        this.tableData.push({
          id: i,
          name: tbData.name + i,
          gender: i % 2 == 0 ? "女" : "男",
          age: tbData.age,
          account: i % 3 == 0 ? 'admin' : 'user',
          city: cities[index],
          region: region[index],
          address: cities[index] + region[index] + '朝阳路' + i + '号',
          date: tbData.date,
          checked: true,
          postalCode: tbData.postalCode + i,
          edit: tbData.edit
        })
      }
      this.loading = false
    },
    getDeliveryData(num){
      this.loading = true
      const tbData = tableBaseData
      for(let i = 0; i < num; i++){
        // 生成0-4的随机整数
        const index = Math.floor(Math.random() * 5)
        this.tableData.push({
          id: i,
          name: tbData.name + i,
          gender: i % 2 == 0 ? "女" : "男",
          phone: `139213${i}64${i}5`,
          city: cities[index],
          region: region[index],
          address: cities[index] + region[index] + '朝阳路' + i + '号',
          date: '2023-08-08',
          postalCode: tbData.postalCode + i,
        })
      }
      this.loading = false
    },
  }
}
